package com.jiangyao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangyao.common.utils.FtpClient;
import com.jiangyao.dao.IJobDao;
import com.jiangyao.model.dto.CollectDto;
import com.jiangyao.model.dto.JobDTO;
import com.jiangyao.model.entity.Collect;
import com.jiangyao.dao.ICollectDao;
import com.jiangyao.model.entity.Job;
import com.jiangyao.model.vo.CollectVo;
import com.jiangyao.model.vo.JobVo;
import com.jiangyao.service.CollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangyao.service.JobDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 收藏与点赞 服务实现类
 * </p>
 *
 * @author xiangyan
 * @since 2020-08-15
 */
@Service
public class CollectServiceImpl extends BaseServiceImpl<ICollectDao, Collect> implements CollectService {
    @Autowired
    private IJobDao jobDao;

    @Autowired
    private ICollectDao collectDao;

    @Autowired
    JobDataService jobDataService;

    @Override
    public Boolean cancelCollectLike(CollectDto dto) {//取消点赞
        Map<String,Object> map = new HashMap<>();
        map.put("type",dto.getType());
        map.put("action",dto.getAction());
        map.put("relate_id",dto.getRelateId());
        map.put("user_id",getLoginApiUserId());
        if(dto.getType()==2){
            map.put("clsub_id",dto.getClsubId());
        }
        this.removeByMap(map);
        if(dto.getType()==1){
            jobDataService.updateCollectNum(dto.getRelateId());
        }
        return true;
    }

    @Override
    public List<CollectVo> selectCollectLikeList(CollectDto collectDto) {
        return collectDao.selectCollectLikeList(collectDto);
    }

    @Override
    public List<CollectVo> getUserCoursewareList(CollectDto dto) {//用户收藏的课件
        dto.setType(2);
        dto.setCurrentUserId(getLoginApiUserId());
        List<CollectVo> result =  collectDao.selectUserCoursewareList(dto);
        if (!CollectionUtils.isEmpty(result)) {
            result.forEach(r -> r.setRelateFullUrl(FtpClient.fullFtpUrl(r.getRelateUrl())));
        }
        return result;
    }


    @Override
    public List<CollectVo> getUserOrgList(CollectDto dto) {//用户收藏的公司
        dto.setType(3);
        dto.setCurrentUserId(getLoginApiUserId());
        List<CollectVo> result =  collectDao.getUserOrgList(dto);
        if (!CollectionUtils.isEmpty(result)) {
            result.forEach(r -> r.setRelateFullUrl(FtpClient.fullFtpUrl(r.getRelateUrl())));
        }
        return result;
    }

    @Override
    public List<CollectVo> getUserJobList(CollectDto dto) {//用户收藏的职位
        QueryWrapper<Collect> params = new QueryWrapper<>();
        params.eq("type",1).eq("action",dto.getAction()).eq("user_id",getLoginApiUserId());
        List<Collect> list = this.list(params);
        if(list==null || list.isEmpty()){
            return null;
        }
        Set<Integer> jobSet = list.stream().collect(Collectors.toMap(Collect::getRelateId, a -> a,(k1, k2)->k1)).keySet();
        JobDTO jobDto = new JobDTO();
        jobDto.setJobIdList(new ArrayList<>(jobSet));
        List<JobVo> jobList = jobDao.getWebJob(jobDto);
        Map<Integer, JobVo> jobMap = jobList.stream().collect(Collectors.toMap(JobVo::getJobId, a -> a,(k1, k2)->k1));
        List<CollectVo> result = new ArrayList<>();
        for(Collect collect:list){
            CollectVo vo = new CollectVo();
            vo.setId(collect.getId());
            vo.setRelateId(collect.getRelateId());
            JobVo jobVo = jobMap.get(collect.getRelateId());
            if(jobVo!=null) {
                vo.setRelateName(jobVo.getJobName());
                vo.setMinSize(jobVo.getMinSize());
                vo.setMaxSize(jobVo.getMaxSize());
                vo.setOrgName(jobVo.getOrgName());
            }
            result.add(vo);
        }
        return result;
    }
}
